 //控制层 
app.controller('specificationController' ,function($scope,$controller   ,specificationService){	
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		specificationService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	};

	//分页
	$scope.findPage=function(){
		specificationService.findPage($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	};

	//查询实体 
	$scope.findOne=function(id){				
		specificationService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	};

	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.spec.id){//如果有ID
			serviceObject=specificationService.update( $scope.entity ); //修改  
		}else{
			serviceObject=specificationService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.findPage();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	};

	//批量删除 
	$scope.dele=function(){
		if ( $scope.selectIds.length <1)
			alert("请至少选中一项！");
		//获取选中的复选框			
		specificationService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.findPage();//刷新列表
					$scope.selectIds=[];//清空数组
				}						
			}		
		);				
	};

    //定义搜索对象
	$scope.searchEntity={};
	//搜索
	$scope.search=function(){
		specificationService.search($scope.paginationConf.currentPage,$scope.paginationConf.itemsPerPage,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	};

	//新建规格选项
	$scope.addOption = function () {
		$scope.entity.options.push({});//添加一项到规格选项数组中
    };

    //删除规格选项
	$scope.delOption = function (index) {
		$scope.entity.options.splice(index,1);
    }
    
});	
